<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
    <title>WebSocket Demo</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        .container {
            width: 800px;
            padding: 20px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .message-input {
            margin-bottom: 10px;
        }
        .message-history {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            height: 800px;
            overflow-y: auto;
            background-color: #f8f8f8;
        }
        .message-item {
            padding: 5px;
            border-bottom: 1px solid #ddd;
        }

        /* 模态框样式 */
        .modal {
            display: none; /* 默认隐藏 */
            position: fixed; /* Stay in place */
            z-index: 1; /* Sit on top */
            padding-top: 100px; /* Location of the box */
            left: 0;
            top: 0;
            width: 100%; /* Full width */
            height: 100%; /* Full height */
            overflow: auto; /* Enable scroll if needed */
            background-color: rgb(0,0,0); /* Fallback color */
            background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
        }

        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
        }

        /* The Close Button */
        .close {
            color: #aaaaaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }

        .status-circle {
            display: inline-block;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            margin-right: 10px;
            vertical-align: middle;
        }

        .status-circle.green {
            background-color: green;
        }

        .status-circle.gray {
            background-color: gray;
        }

        #state {
            font-weight: bold;
        }

        /* ... 之前的样式 ... */

        /* 盘点状态文字的样式 */
        #statusText {
            color: #000000; /* 黑色字体，您可以根据需要更改为其他颜色 */
            display: inline-block; /* or inline */
        }

        /* 当状态为“开始盘点”时，改变字体颜色 */
        #state.start-counting #statusText {
            color: #008000; /* 绿色字体 */
        }

        /* 当状态为“结束盘点”时，改变字体颜色 */
        #state.end-counting #statusText {
            color: #800000; /* 红色字体 */
        }



    </style>
</head>
<body>
<div class="container">
    <input type="text" class="message-input" id="messageInput" placeholder="Enter message..."></input>
    <button onclick="sendMessage()">Send Message</button>
    <span id="state">
        <span id="statusCircle" class="status-circle gray"></span>
         <span id="statusText" class="status-circle green">等待盘点</span>
    </span>
    <ul id="message"></ul>
    <div class="message-history" id="messageHistory"></div>
    <div id="myModal" class="modal">
        <div class="modal-content">
            <span class="close">&times;</span>
            <p id="modalMessage"></p>
        </div>
    </div>
</div>

<script>

    // script.js
    let socket = new WebSocket("ws://localhost:18080/websocket/89");
    let messageHistory = document.getElementById("messageHistory");
    let modal = document.getElementById("myModal");
    let modalMessage = document.getElementById("modalMessage");
    let statusCircle = document.getElementById("statusCircle");
    let statusV1 = document.getElementById("statusText");

    let statusText = document.getElementById("state");


    socket.onopen = function(event) {
        console.log("WebSocket opened");
    };

    socket.onmessage = function(event) {
        console.log("Message from server: " + event.data);
        const message = event.data;

        if (message === '开始盘点') {
            updateStatus('开始盘点', '');
        } else if (message === '结束盘点') {
            updateStatus('结束盘点', '');
        }else{
            showMessage(event.data);
            showModal(event.data); // 显示在模态框中
        }
    };

    socket.onclose = function(event) {
        console.log("WebSocket closed");
    };

    function updateStatus(status, message) {
        if (status === '开始盘点') {
            statusCircle.classList.remove('gray');
            statusCircle.classList.add('green');
            statusText.classList.add('start-counting');
            statusText.textContent = `正在盘点 ${message}`;
        } else if (status === '结束盘点') {
            statusCircle.classList.remove('green');
            statusCircle.classList.add('gray');
            statusText.classList.add('end-counting');
            statusText.textContent = `结束盘点`;
        }
    }

    // 显示模态框的函数
    function showModal(message) {
        modal.style.display = "block";
        modalMessage.textContent = message;

        // 设置定时器在20秒后关闭模态框
        setTimeout(function() {
            modal.style.display = "none";
        }, 20000); // 20秒 = 20000毫秒
    }

    // 隐藏模态框的函数（如果需要手动关闭）
    function hideModal() {
        modal.style.display = "none";
    }

    // 为模态框的关闭按钮添加点击事件监听器
    let span = document.getElementsByClassName("close")[0];
    span.onclick = function() {
        hideModal();
    }

    // 当用户点击任何地方之外模态框时，关闭它
    window.onclick = function(event) {
        if (event.target == modal) {
            hideModal();
        }
    }


    function sendMessage() {
        //let message = document.getElementById("messageInput").value;
      //  socket.send(message);
        // Optionally, you can clear the input field after sending
      //  document.getElementById("messageInput").value = "";
        callGetLatest();
    }

    function showMessage(message) {
        let messageDiv = document.createElement("div");
        messageDiv.className = "message-item";
        messageDiv.textContent = message;
        messageHistory.appendChild(messageDiv);
        messageHistory.scrollTop = messageHistory.scrollHeight; // Scroll to bottom
    }

    function callGetLatest() {
        fetch('/invent/getAsyncReadData')
            .then(response => response.json())
            .then(data => {
                console.log("Data from /invent/getAsyncReadData:", data);
                // 在这里你可以根据需要处理从 /invent/getLatest1 返回的数据
               // showMessage(`Data from /invent/getLatest1: ${JSON.stringify(data)}`);
            })
            .catch(error => console.error("Error calling /invent/getLatest1:", error));
    }

</script>
</body>
</html>